package nc.bs.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import nc.vo.bd.material.MaterialVO;
import nc.vo.pub.BusinessException;

public class MaterialInfoUtil {

	public static String getXLCode(String path) throws IOException,
			BusinessException {
		String xlPath = null;
		if (path.contains("GC3_U")) {
			xlPath = path.replace("GC3_U", "FC1_U");
		}
		if (path.contains("GC2_U")) {
			xlPath = path.replace("GC2_U", "FC1_U");
		}
		if (path.contains("FC2_U")) {
			xlPath = path.replace("FC2_U", "FC1_U");
		}
		if (path.contains("GC4_U")) {
			xlPath = path.replace("GC4_U", "FC1_U");
		}
		if (path.contains("GC7_U")) {
			xlPath = path.replace("GC7_U", "FC1_U");
		}
		if (path.contains("GC5_U")) {
			xlPath = path.replace("GC5_U", "FC1_U");
		}
		if (path.contains("GC6_U")) {
			xlPath = path.replace("GC6_U", "FC1_U");
		}
		if (path.contains("KC2_U")) {
			xlPath = path.replace("KC2_U", "FC1_U");
		}
		File file = new File(xlPath);
		String XLCode = null;
		String encoding = "UTF-8";
		ArrayList<String> fileInfo = new ArrayList();
		if (file.isFile() && file.exists()) {

			InputStreamReader read = new InputStreamReader(new FileInputStream(
					file), encoding);
			BufferedReader bf = new BufferedReader(read);
			String line = null;
			while ((line = bf.readLine()) != null) {
				if (line.startsWith("[XL]")) {
					String[] temp = line.split(";");
					XLCode = temp[1].trim();
					break;
				}
			}
			bf.close();

		}
		String xl = BaseDataUtil.getNCMaterialXLCode(XLCode);
		return xl;
	}

	public static String getSpecialXLCode(String path) throws IOException {

		String xlPath = path.replace("GC4_U ", "FC1_U");
		File file = new File(xlPath);
		String XLCode = null;
		String encoding = "UTF-8";
		ArrayList<String> fileInfo = new ArrayList();
		if (file.isFile() && file.exists()) {

			InputStreamReader read = new InputStreamReader(new FileInputStream(
					file), encoding);
			BufferedReader bf = new BufferedReader(read);
			String line = null;
			while ((line = bf.readLine()) != null) {
				if (line.startsWith("[XL]")) {
					String[] temp = line.split(";");
					XLCode = temp[1].trim();
				}
			}

		}
		return XLCode;
	}

	public static MaterialVO getAlumDada(String path) throws Exception {
		String filePath = path.replace("FC2_U", "FC4_U");
		if (!new File(filePath).exists()) {
			return null;
		}
		ArrayList list = Klaes2NCAlumMaterial.getDataFromKlaesFile(filePath);
		MaterialVO mVO = Klaes2NCAlumMaterial.getMaterialArray(list,null);
		return mVO;
	}
}
